package io.renren.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.hutool.core.util.StrUtil;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.dao.AutogroupDao;
import io.renren.dto.AutogroupDTO;
import io.renren.entity.AutobrandEntity;
import io.renren.entity.AutogroupEntity;
import io.renren.service.AutogroupService;
import io.renren.service.AutomodelsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * ${comments}
 *
 * @author Sen 524227263@qq.com
 * @since 1.0.0 2024-05-27
 */
@Service
public class AutogroupServiceImpl extends CrudServiceImpl<AutogroupDao, AutogroupEntity, AutogroupDTO> implements AutogroupService {

    @Autowired
    private AutomodelsService automodelsService;

    @Override
    public QueryWrapper<AutogroupEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<AutogroupEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public List<Map<String, Object>> getCarGroupList(String autoBrandFk) {

        List<Map<String, Object>> res = new ArrayList<>();

        QueryWrapper<AutogroupEntity> wrapper = new QueryWrapper<>();
        wrapper.select("BrandVendor,AutoGroupOID,Name,Code,AutoBrand_FK");
        wrapper.eq("AutoBrand_FK", autoBrandFk);
        wrapper.apply("(StopDate is  null or StopDate = '1900-01-01') and (BrandVendor is not null and BrandVendor != '')");
        wrapper.orderByAsc("Name");
        List<AutogroupEntity> list = this.baseDao.selectList(wrapper);

        for (AutogroupEntity info : list) {
            BigDecimal min = automodelsService.selectMinOneByAutoGroupFK(info.getAutogroupoid());
            BigDecimal max = automodelsService.selectMaxOneByAutoGroupFK(info.getAutogroupoid());
            List<BigDecimal> priceRange = new ArrayList<>();
            priceRange.add(min);
            priceRange.add(max);
            info.setPriceRange(priceRange);
        }

        Map<String, List<AutogroupEntity>> groupedItems = list.stream()
                .collect(Collectors.groupingBy(AutogroupEntity::getBrandvendor));

        groupedItems.forEach((category, itemsInCategory) -> {
            Map<String, Object> map = new HashMap<>();
            map.put("name", category);
            map.put("list", itemsInCategory.stream().map(item -> {
                Map<String, Object> mapItem = new HashMap<>();
                mapItem.put("BrandVendor", item.getBrandvendor());
                mapItem.put("AutoGroupOID", item.getAutogroupoid());
                mapItem.put("Name", item.getName());
                mapItem.put("Code", item.getCode());
                mapItem.put("AutoBrand_FK", item.getAutobrandFk());
                mapItem.put("img", item.getImg());
                mapItem.put("priceRange", item.getPriceRange());
                return mapItem;
            }).collect(Collectors.toList()));

            res.add(map);
        });

        return res;
    }

    @Override
    public AutogroupEntity findOneByAutoGroupOID(String autogroupFk) {
        QueryWrapper<AutogroupEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("AutoGroupOID", autogroupFk);
        List<AutogroupEntity> list = baseDao.selectList(wrapper);
        return list.size() > 0 ? list.get(0) : null;
    }
}